package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mchange.v2.beans.BeansUtils;
import com.offcn.entity.pet_system;
import com.offcn.service.Pet_SystemService;
import com.offcn.service.Pet_SystemServiceImpl;
import com.offcn.utlis.BaseServlet;
import com.offcn.utlis.PageUtils;
import com.offcn.utlis.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/pet")
public class Pet_SystemServlet extends BaseServlet {
    //调用业务层
    Pet_SystemService petSystemService = new Pet_SystemServiceImpl();
    // 工具 提示
    ResultVo resultVo = null;
    // 调用 json
    ObjectMapper objectMapper = new ObjectMapper();

    //分页
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search");
        int currentPage = Integer.parseInt(req.getParameter("currentPage")); //当前页
        int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 条数

       PageUtils<pet_system> pageUtils =  petSystemService.find(search,currentPage,pageSize);
       if (pageUtils!=null){
           resultVo = new ResultVo(200,"查询成功",pageUtils);
       }else {
           resultVo = new ResultVo(500,"查询失败",null);
       }
       resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 删除
    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pid = req.getParameter("pid");

        boolean b = petSystemService.delfind(pid);
         if (b){
             resultVo = new ResultVo(200,"删除成功",null);
         }else {
             resultVo = new ResultVo(500,"删除失败",null);
         }
         resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }
    // 修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取前端发来的请求
        pet_system petSystem = new pet_system();

        BeanUtils.populate(petSystem,req.getParameterMap());
        // 数据库中查询，返回结果
       boolean k =petSystemService.updateByid(petSystem);
        // 判断
       if (k){
           resultVo = new ResultVo(200,"修改成功",null);
       }else {
           resultVo = new ResultVo(500,"修改失败",null);
       }
       // 回传数据
       resp.getWriter().println(objectMapper.writeValueAsString(resultVo));

    }

    //添加
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        // 获取前端发来请求
        pet_system petSystem = new pet_system();
          BeanUtils.populate(petSystem,req.getParameterMap());
         boolean b = petSystemService.add(petSystem);
         if (b){
             resultVo = new ResultVo(200,"添加成功",null);
         }else {
             resultVo = new ResultVo(500,"添加失败",null);
         }
         resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }
}
